import org.apache.spark.SparkConf;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.types.StructType;

/**
 * Created by chs on 8/23/18.
 */
public class JDAnalysis {

    public static void main(String[] args){
      String path = "hdfs://master:9000/warehouse";

        SparkConf conf = new SparkConf().setAppName("JDAnalysis");

        SparkSession spark = SparkSession.builder().config(conf).getOrCreate();

//        Dataset<Row> userDs = spark.read().csv(path+"/t_user");
        Dataset<Row> orderDs = spark.read().csv(path+"/t_order");
        Dataset<Row> loanDs = spark.read().csv(path+"/t_loan");

        StructType orderSchema = new StructType()
                .add("uid", "string", false)
                .add("buy_time", "string", false)
                .add("price", "string", true)
                .add("qty", "string", false)
                .add("cate_id", "string", false)
                .add("discount", "string", false);

        StructType loanSchema = new StructType()
                .add("uid", "string", false)
                .add("loan_time", "string", false)
                .add("loan_amount", "string", false)
                .add("plannum", "string", true);
        Dataset<Row> orderDf = spark.createDataFrame(orderDs.toJavaRDD(), orderSchema);
        Dataset<Row> loanDf = spark.createDataFrame(loanDs.toJavaRDD(), loanSchema);
//        loanDf.filter("loan_amount>5")
//                .join(orderDf,"uid")
//                .select("uid","loan_amount","price","qty","discount")
//                .where("(price*qty-discount)>loan_amount")
//                .show();


        orderDf.filter("discount=0")
                .select("uid")
                .join(loanDf,orderDf.col("uid").equalTo(loanDf.col("uid")),"left")
                .where(loanDf.col("uid").isNull())
                .show();
//        dataFrame.createOrReplaceTempView("loan");
//        spark.sql("select uid,loan_amount from loan where loan_amount>5").show();
    }


}
